package mis.ds.ps.ui.swing;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import mis.ds.ps.controller.Result;
import mis.ds.ps.controller.Session;
import mis.ds.ps.model.Mapper;
import mis.ds.ps.model.obj.Docentes;
import mis.ds.ps.model.obj.Mappercomposto;
import mis.ds.ps.model.obj.Modulos;
import mis.ds.ps.model.obj.Versoes;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.hibernate.loader.custom.Return;

public class SwingAdapter implements ISwingAdapter {

	private static Logger logger = Logger.getLogger(SwingAdapter.class);

	private Session session;
	private List<String> dbVersionsList;
	private List<String> dbMapCompList;
	private List<String> dbModulosList;
	private List<String> dbDocentesList;

	public void createNewSession(String name) {

		PropertyConfigurator.configure("log4j.properties");

		// Create session
		setSession(new Session(name));

		// Set existing database versions
		dbVersionsList = new LinkedList<String>();
		dbMapCompList = new LinkedList<String>();
		dbModulosList = new LinkedList<String>();
		dbDocentesList = new LinkedList<String>();

	}

	public void setSession(Session session) {
		this.session = session;
	}

	public Session getSession() {
		return session;
	}

	public void setDbVersionsList(List<String> dbVersions) {
		this.dbVersionsList = dbVersions;
	}

	public List<String> getDBVersionsList() {

		if (session == null) {
			return dbVersionsList;
		}

		List<Versoes> vsList = session.getVersoesOnSession();

		if (vsList.size() <= 0) {
			logger.debug("Versions List is empty!");
			return dbVersionsList;
		}

		// dbVersionsList.remove(0);
		for (Versoes versao : vsList) {
			dbVersionsList.add(versao.getNome());
		}

		return dbVersionsList;
	}

	public List<String> getDBMapCompList() {

		if (session == null) {
			return dbMapCompList;
		}

		dbMapCompList.clear();

		List<Mappercomposto> mcList = session.getMapCompOnSession();

		if (mcList.size() <= 0) {
			logger.debug("Composed Map List is empty!");
			return dbMapCompList;
		}

		for (Mappercomposto mc : mcList) {
			dbMapCompList.add(mc.getNome());
		}

		return dbMapCompList;
	}

	public boolean setImportFile(String fileName) {
		return session.setFileToImport(fileName);
	}

	public Result simulate() {

		return session.testSimulate();

	}
	
	public void simulateModule2() {
		session.testSimulateModule2();
	}

	public List<String> getSheetList() {

		List<String> sheetNames = new LinkedList<String>();

		sheetNames.add("No available sheets");
		if (session == null || session.getFileSheets().size() <= 0) {
			logger.debug("File has no Sheets!");
			return sheetNames;
		}

		sheetNames.remove(0);
		for (Iterator<String> it = session.getFileSheets().iterator(); it.hasNext();) {
			sheetNames.add(it.next());
		}

		return sheetNames;
	}

	public void setSheet(String sheetName) {
		session.setSheetName(sheetName);
	}

	public boolean createCompositeMapper(String compMapperName) {
		return session.createCompMapper(compMapperName);
	}

	public Mapper getCompositeMapper(String name) {
		return session.getCompMapperInUse(name);
	}
	
	public List<String> getDBModulosList() {
		if (session == null) {
			return dbModulosList;
		}
		
		List<Modulos> lstModulos = session.getModulosOnSession();
		
		if (lstModulos.size() <= 0) {
			logger.debug("Modulos List is empty!");
			return dbModulosList;
		}
		
		for (Modulos modulo : lstModulos) {
			dbModulosList.add(modulo.getNome());
		}
		
		return dbModulosList;
 	}
	
	public List<String> getDBDocentesList() {
		if (session == null) {
			return dbDocentesList;
		}
		
		List<Docentes> lstDocentes = session.getDocentesOnSession();
		
		if (lstDocentes.size() <= 0) {
			logger.debug("Docentes List is empty!");
			return dbDocentesList;
		}
		
		for (Docentes docente : lstDocentes) {
			dbDocentesList.add(docente.getNome());
		}
		
		return dbDocentesList;
 	}
	
}
